package com.jhclothing.web.controller.outbound;

import com.jhclothing.common.annotation.Log;
import com.jhclothing.common.core.controller.BaseController;
import com.jhclothing.common.core.domain.AjaxResult;
import com.jhclothing.common.core.page.TableDataInfo;
import com.jhclothing.common.enums.BusinessType;
import com.jhclothing.common.utils.StringUtils;
import com.jhclothing.common.utils.poi.ExcelUtil;
import com.jhclothing.outbound.domain.ClothingOutbound;
import com.jhclothing.outbound.service.IClothingOutboundService;
import com.jhclothing.product.domain.ClothingType;
import com.jhclothing.product.service.IClothingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 服饰出库Controller
 *
 * @author zyp
 * @date 2024-04-25
 */
@RestController
@RequestMapping("/outbound/clothing")
public class ClothingOutboundController extends BaseController {
    @Autowired
    private IClothingOutboundService clothingOutboundService;
    @Autowired
    private IClothingTypeService clothingTypeService;

    /**
     * 查询服饰出库列表
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClothingOutbound clothingOutbound) {
        startPage();
        List<ClothingOutbound> list = clothingOutboundService.selectClothingOutboundList(clothingOutbound);
        return getDataTable(list);
    }

    /**
     * 导出服饰出库列表
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:export')")
    @Log(title = "服饰出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClothingOutbound clothingOutbound) {
        List<ClothingOutbound> list = clothingOutboundService.selectClothingOutboundList(clothingOutbound);
        ExcelUtil<ClothingOutbound> util = new ExcelUtil<>(ClothingOutbound.class);
        util.exportExcel(response, list, "服饰出库数据");
    }

    /**
     * 获取服饰出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:query')")
    @GetMapping(value = {"/", "/{clothingOutboundId}"})
    public AjaxResult getInfo(@PathVariable(value = "clothingOutboundId", required = false) Long clothingOutboundId) {
        AjaxResult ajax = AjaxResult.success();
        List<ClothingType> clothingTypes = clothingTypeService.selectClothingTypeAll();
        ajax.put("clothingTypes", clothingTypes);
        if (StringUtils.isNotNull(clothingOutboundId)) {
            ClothingOutbound clothingoutbound = clothingOutboundService.selectClothingOutboundByOutboundId(clothingOutboundId);
            ajax.put(AjaxResult.DATA_TAG, clothingoutbound);
            ajax.put("clothingTypeId", clothingoutbound.getClothingInventory().getClothingTypeId());
            ajax.put("clothingSize", clothingoutbound.getClothingInventory().getClothingSize());
        }
        return ajax;
    }

    /**
     * 新增服饰出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:add')")
    @Log(title = "服饰出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothingOutbound clothingOutbound) {
        clothingOutbound.setCreateBy(getUsername());
        String result = clothingOutboundService.insertClothingOutbound(clothingOutbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }

    /**
     * 修改服饰出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:edit')")
    @Log(title = "服饰出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothingOutbound clothingOutbound) {
        clothingOutbound.setUpdateBy(getUsername());
        String result = clothingOutboundService.updateClothingOutbound(clothingOutbound);
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }

    /**
     * 删除服饰出库
     */
    @PreAuthorize("@ss.hasPermi('outbound:clothing:remove')")
    @Log(title = "服饰出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clothingOutboundIds}")
    public AjaxResult remove(@PathVariable Long[] clothingOutboundIds) {
        String result = clothingOutboundService.deleteClothingOutboundByOutboundIds(clothingOutboundIds, getUsername());
        if (!"SUCCESS".equals(result)) {
            return error(result);
        }
        return AjaxResult.success();
    }
}
